using System;
using System.Collections.Generic;
using Main.CropPlant;
using Main.Inventory;
using Main.Map;
using UnityEngine;
using ZSW.Framework;
using ZSW.Framework.Utils;

namespace Main.Details
{
    [Serializable]
    public class ItemDetails
    {
        [Tooltip("物品ID")]
        public int ItemID = 10;
        [Tooltip("物品名称")]
        public string ItemName;
        [Tooltip("物品类型")]
        public ItemType ItemType;
        [Tooltip("物品图标")]
        public Sprite ItemIcon;
        [Tooltip("物品在世界上的图标")]
        public Sprite ItemOnWorldSprite;
        [Tooltip("物品描述")]
        public string ItemDescription;
        [Tooltip("物品使用范围")]
        public int ItemUseRadius;
        [Tooltip("物品是否可以捡起")]
        public bool CanPickUp;
        [Tooltip("物品是否可以丢弃")]
        public bool CanDropped;
        [Tooltip("物品是否可以举起")]
        public bool CanCarried;
        [Tooltip("物品价格")]
        public int ItemPrice;
        [Tooltip("物品卖出降价百分比")]
        [Range(0, 1)]
        public float SellPercentage = 0.7f;
    }

    /// <summary>
    /// 背包中的数据结构
    /// </summary>
    [Serializable]
    public struct InventoryItem
    {
        public int ItemID;
        public int ItemAmount;
        public void SetItemID(int id)
        {
            ItemID = id;
        }
        public void AddItem(int amount)
        {
            ItemAmount += amount;
            Debug.Log(ItemAmount);
            if (ItemAmount < 0)
            {
                ZSWFUtil_Debug.Log("InventoryItem", $"{ItemID} 物品数量不能为负数！");
            }
        }
    }

    [Serializable]
    public class AnimationType
    {
        public PartType partType;
        public PartName partName;
        public AnimatorOverrideController controller;
    }

    [Serializable]
    public class SerializableVector3
    {
        public float x;
        public float y;
        public float z;


        public SerializableVector3(Vector3 vector)
        {
            x = vector.x;
            y = vector.y;
            z = vector.z;
        }

        public Vector3 ToVector3()
        {
            return new Vector3(x, y, z);
        }

        public Vector2Int ToVector2Int()
        {
            return new Vector2Int((int)x, (int)y);
        }
    }

    [Serializable]
    public class SceneItem
    {
        public int itemID;
        public SerializableVector3 position;
    }

    [Serializable]
    public class SceneFurniture
    {
        public int itemID;
        public SerializableVector3 position;
        public int boxIndex;
    }

    [Serializable]
    public class TileProperty
    {
        public Vector2Int tileCoordinate;
        public GridType gridType;
        public bool boolTypeValue;
    }

    [Serializable]
    public class TileDetails
    {
        public int girdX;
        public int girdY;
        public bool canDig;
        public bool canDropItem;
        public bool canPlaceFurniture;
        public bool isNPCObstacle;

        /// <summary>
        /// 挖坑后过了几天
        /// </summary>
        public int daysSinceDug = -1;

        /// <summary>
        /// 浇水后过了几天
        /// </summary>
        public int daysSinceWatered = -1;

        /// <summary>
        /// 种子ID
        /// </summary>
        public int seedItemID = -1;

        /// <summary>
        /// 种子成长天数
        /// </summary>
        public int growDays = -1;

        /// <summary>
        /// 距离上一次收割的时间
        /// </summary>
        public int daysSinceLastHarvest = -1;

        /// <summary>
        /// 设置属性
        /// </summary>
        internal void SetProperty(TileProperty tileProperty)
        {
            switch (tileProperty.gridType)
            {
                case GridType.Digable:
                    this.canDig = tileProperty.boolTypeValue;
                    break;
                case GridType.DropItem:
                    this.canDropItem = tileProperty.boolTypeValue;
                    break;
                case GridType.NPCObstacle:
                    this.isNPCObstacle = tileProperty.boolTypeValue;
                    break;
                case GridType.PlaceFurniture:
                    this.canPlaceFurniture = tileProperty.boolTypeValue;
                    break;
            }
        }

        /// <summary>
        /// 判断物品是否与当前物体匹配
        /// </summary>
        /// <param name="item"></param>
        /// <param name="crop"></param>
        /// <returns></returns>
        internal bool IsValid(ItemDetails item, Crop crop, Vector3 mouseWorldPos)
        {
            var itemType = item.ItemType;
            //WORKFLOW:物品类型和当前时间格子类型是否匹配
            switch (itemType)
            {
                case ItemType.Seed:
                    return daysSinceDug > -1 && daysSinceWatered > -1 && seedItemID == -1;
                case ItemType.Commodity:
                    return this.canDropItem;
                case ItemType.Furniture:
                    // 可以放置家具 && 需求物品是否拥有 && 周围是否有此物品
                    if (this.canPlaceFurniture && ZSWF_IOCContainer.Get<InventoryManager>().CheckBluePrintStock(item.ItemID))
                    {
                        return true;
                    }
                    return false;
                case ItemType.HoeTool:
                    return this.canDig;
                case ItemType.WaterTool:
                    return daysSinceDug > -1 && daysSinceWatered == -1;
                case ItemType.BreakTool:
                case ItemType.ChopTool:
                    // Debug.Log($"Crop: {crop} Harverst: {crop.IsHarvest} ");
                    return crop != null && crop.IsHarvest && crop.cropDetails.ToolIsValid(item.ItemID);
                case ItemType.CollectTool:
                    return crop != null && growDays > crop.cropDetails.DayAmount && crop.cropDetails.ToolIsValid(item.ItemID);
                case ItemType.ReapTool:
                    return ZSWF_IOCContainer.Get<GridMapManager>().HaveReapableItemsInRadius(mouseWorldPos, item);
            }
            return false;
        }
    }

    [Serializable]
    public class NPCPosition
    {
        public Transform npc;
        public string starScene;
        public Vector3 position;
    }

    [Serializable]
    public class SceneRoute
    {
        public string name;
        public string sceneName;
        public string gotoSceneName;
        public List<ScenePath> scenePathList;
    }

    [Serializable]
    public class ScenePath
    {
        public string sceneName;
        public Vector2Int fromGridCell;
        public Vector2Int gotoGridCell;
    }
}